import { searchCover, isString, isNumber } from "@/common.js"
// 处理用户的输入
export const handleInput = function (e, field) {
  let value = isString(e) || isNumber(e) ? e : e.target.value
  let errmsg = value.length > this.item.maxlength ? `文本最大长度为${this.item.maxlength}` : ''
  // 正则校验
  // const reg = eval(`new RegExp(${this.item.regExp})`)
  // const flag = reg.test(value)
  value = value.slice(0, this.item.maxlength)
  const item = {...this.item, [field]: value, errmsg}
  this.$emit('setItem', item)
  let formData = searchCover(this.formData, {id: this.item.id}, v => ({...v, [field]: value, errmsg}))
  formData = formData.map(v => ({...v, children: v.type == 'split' ? searchCover(v.children, {id: this.item.id}, v => ({...v, [field]: value, errmsg})) : v.children}))
  this.$emit('setFormData', formData)
  eval(this.item.onChange)(e)
}